package com.atguigu.spzx.manager.service.Impl;

import com.atguigu.spzx.manager.mapper.BrandMapper;
import com.atguigu.spzx.manager.service.BrandService;
import com.atguigu.spzx.model.entity.product.Brand;
import com.atguigu.spzx.service.exception.GuliException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: BrandServiceImpl
 * Package: com.atguigu.spzx.manager.service.Impl
 * Description:
 *
 * @Author 刘强
 * @Create 2024/7/5 14:40
 * @Version 1.0
 */
@Service
public class BrandServiceImpl implements BrandService {
    @Resource
    private BrandMapper brandMapper;

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Brand> page(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Brand> list = brandMapper.page(pageNum, pageSize);

        return new PageInfo<>(list);
    }

    /**
     * 新增品牌
     *
     * @param brand
     */
    @Override
    public void save(Brand brand) {
        brandMapper.save(brand);
    }

    /**
     * 根据id修改
     *
     * @param brand
     */
    @Override
    public void updateById(Brand brand) {
        if (brand.getId() != null) {
            brandMapper.updateById(brand);
        }
    }

    /**
     * 根据id删除
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        if (id == null) {
            throw new GuliException(201, "修改失败");
        }
        brandMapper.deleteById(id);
    }

    /**
     * 查询所有品牌
     * @return
     */
    @Override
    public List<Brand> findAll() {
       List<Brand> list = brandMapper.findAll();
        return list;
    }
}
